package com.efast.dmom.mgmt.service.master.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.dao.master.TmBasDataCollectionItemMapper;
import com.efast.dmom.mgmt.entity.master.TmBasDataCollection;
import com.efast.dmom.mgmt.entity.master.TmBasDataCollectionItem;
import com.efast.dmom.mgmt.service.master.TmBasDataCollectionItemService;
import com.efast.dmom.mgmt.service.master.TmBasDataCollectionService;

/**
 * <p>
 * 数据采集器数据项定义 服务实现类
 * </p>
 *
 * @author Xu Cong
 * @since 2018-09-20
 */
@Service
public class TmBasDataCollectionItemServiceImpl extends ServiceImpl<TmBasDataCollectionItemMapper, TmBasDataCollectionItem> implements TmBasDataCollectionItemService {

	@Autowired
	private TmBasDataCollectionService tmBasDataCollectionService;
	
	@Autowired
	private TmBasDataCollectionItemMapper tmBasDataCollectionItemMapper;
	
	@Override
	public void save(TmBasDataCollectionItem tmBasDataCollectionItem) {
		TmBasDataCollection tmBasDataCollection = tmBasDataCollectionService.selectById(tmBasDataCollectionItem.getDataCollectionId());
		if(tmBasDataCollection!=null) {
			tmBasDataCollectionItem.setDataCollectionName(tmBasDataCollection.getDataCollectionName());
		}
		tmBasDataCollectionItem.setCreateTime(new Date());
		tmBasDataCollectionItem.setCreateUser(SpringWebUtils.getRequestUser());
		tmBasDataCollectionItem.setCompanyCode(SpringWebUtils.getRequestCompany());
		this.insert(tmBasDataCollectionItem);
	}
	
	@Override
	public Map<Integer, Object> getTmBasDciByIds(String dcItemIds) {
		Map<Integer,Object> map = new HashMap<Integer,Object>();
		EntityWrapper<TmBasDataCollectionItem> wrapper = new EntityWrapper<>();
		// List<TmBasDataCollectionItem> dciList = selectList(wrapper.addFilter(" item_id in", "("+dcItemIds+")"));
		List<String> asList = Arrays.asList(dcItemIds.split(","));
		List<Integer> list =new ArrayList<Integer>();
		List<TmBasDataCollectionItem> dciList = null;
		for(String str : asList) {
			  Integer i = Integer.parseInt(str);
			  list.add(i);
			}
		if(list!=null&&list.size()>0) {
			dciList = tmBasDataCollectionItemMapper.selectList(list);
		}
		if(dciList!=null&&dciList.size()>0) {
			for(TmBasDataCollectionItem dci:dciList) {
				map.put(dci.getItemId(), dci);
			}
		}
		return map;
	}

}
